import modelExtend from 'dva-model-extend';
import { pageModel } from './common';
import queryString from 'query-string';
import { query, update, getPermission } from "./../services/permissions";
import * as menuService from "./../services/menu";

export default modelExtend(pageModel,{
    namespace: 'permissions',
    state: {
        modalVisible: false,
        modalType: 'update',
        menuList: [],
        userID: '',
        userPermissions: ['1'],
    },
    reducers: {
        showModal (state, { payload }) {
            return { ...state, ...payload, modalVisible: true }
        },
        hideModal (state) {
            return { ...state, modalVisible: false }
        }
    },
    effects: {
        * query ({ payload = {} }, { call, put }) {
            const data = yield call(query, payload);
            const menu = yield  call(menuService.query, payload);
            if (data.success && menu.success){
                yield put({
                    type: 'updateState',
                    payload: {
                        menuList: menu.data,
                    },
                });
                yield put({
                    type: 'querySuccess',
                    payload: {
                        list: data.data,
                        menuList: menu.data,
                        pagination: {
                            current: Number(payload.page) || 1,
                            pageSize: Number(payload.pageSize) || 10,
                            total: data.total,
                        },
                    },
                });
            }
        },

        * update ({ payload }, { select, call, put }) {
            const { userID } = yield select(_ => _.permissions);
            const newPermissions = { ...payload, id:userID };
            const data = yield call(update, { newPermissions });
            if (data.success) {
                yield put({ type: 'hideModal' });
                yield put({ type: 'query' })
            } else {
                throw data
            }
        },

        * getPermission({ payload }, { call, put }){
            const { id, modalVisible } = payload;
            const data = yield call(getPermission, { id });
            if(data.success){
                yield put({
                    type: 'updateState',
                    payload: {
                        userPermissions: data.data,
                        modalVisible: modalVisible
                    }
                })
            } else {
                throw data
            }
        }
    },
    subscriptions: {
        setup ({ dispatch, history }) {
            history.listen((location) => {
                if (location.pathname === '/permissions') {
                    const payload = queryString.parse(location.search) || { page: 1, pageSize: 10 };
                    dispatch({
                        type: 'query',
                        payload,
                    })
                }
            })
        },
    }
});